package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.dto.PageDto;
import com.qf.pojo.DtsAd;
import com.qf.pojo.DtsTopic;
import com.qf.service.DtsTopicService;
import com.qf.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/25 12:44
 */

@RestController
@RequestMapping("/admin/topic")
@Slf4j
@CrossOrigin
public class TopicController {
    @Autowired
    private DtsTopicService dtsTopicService;
    //http://localhost:8080/admin/topic/list?page=1&limit=20&title=ss&subtitle=ss&sort=add_time&order=desc
   @GetMapping("/list")
    public  Object topicList(PageDto pageDto){
            Page<DtsTopic> dtsTopicPage= dtsTopicService.selectPage(pageDto);
       List<DtsTopic> records = dtsTopicPage.getRecords();
       long total = dtsTopicPage.getTotal();
       HashMap<String, Object> data = new HashMap<>();
       data.put("total", total);
       data.put("items", records);
       return ResponseUtil.ok(data);

   }

    @PostMapping("/update")
    public  Object updateTopic(@RequestBody DtsTopic dtsTopic){
        dtsTopic.setUpdateTime(new Date());
        dtsTopicService.update(dtsTopic);
        return ResponseUtil.ok(dtsTopic);
    }
    @PostMapping("/create")
    public  Object createTopic(@RequestBody DtsTopic dtsTopic){
        dtsTopic.setUpdateTime(new Date());
        dtsTopic.setAddTime(new Date());
        dtsTopic.setDeleted(false);
        dtsTopicService.insert(dtsTopic);
        return ResponseUtil.ok(dtsTopic);
    }
    @PostMapping("/delete")
    public  Object deleteTopic(@RequestBody DtsTopic dtsTopic){
        dtsTopic.setUpdateTime(new Date());
        dtsTopic.setDeleted(true);
        dtsTopicService.delete(dtsTopic);
        return ResponseUtil.ok();}

}
